package com.example.weather_location_service.service;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.os.SystemClock;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.weather_location_service.WeatherActivity;
import com.example.weather_location_service.util.HttpUtil;
import com.example.weather_location_service.util.Utility;
import com.example.weather_location_service.util.Weatherbean;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class AutoUpateService extends Service {
    public AutoUpateService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        updateWeather();
        loadBingPic();
        //定时任务技巧
        AlarmManager manager=(AlarmManager)getSystemService(ALARM_SERVICE);//报警管理器
        int anHour=1*60*60*1000;//这是一小时毫秒数
        long triggerAtTime= SystemClock.elapsedRealtime()+anHour;
        Intent i=new Intent(this,AutoUpateService.class);
        PendingIntent pendingIntent=PendingIntent.getService(this,0,i,0);
        manager.cancel(pendingIntent);//删除所有与匹配的警报Intent。
        manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,triggerAtTime,pendingIntent);
        return super.onStartCommand(intent, flags, startId);
    }
    /*
    更新天气信息
     */
    private void updateWeather(){
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    String watherString = preferences.getString("weather", null);
        if (watherString != null) {
//            有缓存时直接解析天气数据
            Weatherbean weatherbean = Utility.handleWeatherResponse(watherString);
            String weatherId = weatherbean.getHeWeather().get(0).getBasic().getId();
            String weatherUrl = "http://guolin.tech/api/weather?cityid=" + weatherId + "&key=ddc74d3d55724ca5a4e8631422c27362";
            HttpUtil.sendOKHttpRequest(weatherUrl, new Callback() {
                @Override
                public void onFailure(@NotNull Call call, @NotNull IOException e) {
                    e.printStackTrace();
                }

                @Override
                public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                    final String responseText = response.body().string();
                    final Weatherbean weatherbean = Utility.handleWeatherResponse(responseText);
                            List<Weatherbean.HeWeatherBean> heWeatherBeanList = weatherbean.getHeWeather();
                            if (weatherbean != null && "ok".equals(heWeatherBeanList.get(0).getStatus())) {
                                SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(AutoUpateService.this).edit();
                                editor.putString("weather", responseText);
                                editor.apply();
                            }
                }
            });
        }
    }
    /*
    更新每日一图
     */
    private void loadBingPic() {
        String requsetBingPic = "http://guolin.tech/api/bing_pic";
        HttpUtil.sendOKHttpRequest(requsetBingPic, new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                final String bingPic = response.body().string();
                SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(AutoUpateService.this).edit();
                editor.putString("bing_pic", bingPic);
                editor.apply();
            }
        });
    }

}
